<?php
require_once './application/core/CoreBase.php';
require_once './application/core/Client/configSoap.php';
require_once './application/models/Tables/cache.php';
require_once './application/models/Objets/Composant.php';
require_once './application/entity/config/restore.php';

class restoreCore extends CoreBase
{

    private $_identity;
    private $_ip;
    private $_conf;
    private $_confId;
    private $_model;
    private $_type;
    private $_obj;
    private $_data;
    private $_proto;

    public function restore()
    {
        $this->_ip = $this->_controller->getRequest()->getParam( 'ip' );
        $this->_model = $this->_controller->getRequest()->getParam( 'model' );
        $this->_type = $this->_controller->getRequest()->getParam( 'type' );
        $this->_confId = $this->_controller->getRequest()->getParam( 'conf' );
        $this->_identity = $this->_controller->getRequest()->getParam( 'identity' );
        $this->_proto = $this->_controller->getRequest()->getParam( 'proto' );
        if ( stristr( $this->_proto, "auto" ) )
            $this->_proto = "ssh,telnet";
        $obj = new CoreBase( $this->_controller );
        $this->_identity = $obj->getUsernameBase( $this->_identity );
        $this->_ip = $obj->parseArrayIp( $this->_ip );
        $this->_conf = $this->getConfBase( $this->_confId );
        $params = array( 'Ip'=> $this->_ip,
                'Service' => 'restore',
                'Model'=> $this->_model,
                'Type' => $this->_type,
                'Name' => $this->_confId,
                'Identity' => $this->_identity,
                'Protocol' => $this->_proto,
                'Conf' => $this->_conf );
        $c = new configSoap( $this->_controller );
        $c->restore( $params, 'restoreresp' );
        $this->MiseEnAttenteReponse(true, '/config/restoreresp', null);
    }

    private function getConfBase()
    {
        $this->_obj = new ObjectComposant();
        $this->_conf  = $this->_obj->rechercheConfigurationId( $this->_confId );
        $this->_conf = base64_encode( $this->_conf[0]['Configuration'] );
        return $this->_conf;
    }

    public function restoreResp()
    {
        $cache = new cache();
        $rst = $cache->getInfo( $this->_controller->_idClient, "restoreresp" );
        if ( $rst != false )
        {
            $cache->deleteInfo( $this->_controller->_idClient, 'restoreresp', $rst[0]['Id'] );
            $this->_obj = $this->parseRestoreResp( $rst[0]['Xml'] );
            $this->MiseEnAttenteReponse( false, '/config/restoreresp', $this->_obj );
            return true;
        }
        else
            $this->MiseEnAttenteReponse( true, '/config/restoreresp', null );
    }

    private function parseRestoreResp( $xml )
    {
        $this->_obj = new xmlToObjArray();
        $this->_data = $this->_obj->parse_xml_data( $xml, false );
        $this->_obj = new restoreData();
        $this->_obj->result = $this->_data[0]['PARAM'][0]['RESTORE'][0]['DATA'][0][0];
        return $this->_obj;
    }

}
